package com.atguigu.juc.chap06;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author Taeyang
 * @date 2025/4/1
 */
class MyRunnableThread implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName() + "：Runnable is running");
    }
}

class MyCallableThread implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        for (int i = 0; i < 10; i++) {
            Thread.sleep(500);
            System.out.println(Thread.currentThread().getName() + "：Callable is running");
        }
        return new Random().nextInt(100);
    }
}

public class CallableDemo {

    // 版本V3
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // FutureTask：一个FutureTask对象，线程只执行一次
        FutureTask<Integer> futureTask = new FutureTask<>(new MyCallableThread());
        Thread callable = new Thread(futureTask, "Callable01");
        callable.start();

        // 判断异步线程是否执行完毕
        System.out.println("before get isDone：" + futureTask.isDone());
        // 判断异步线程是否被取消
        System.out.println("before get isCancelled：" + futureTask.isCancelled());

        Thread.sleep(3000);
        // 取消异步线程：参数：线程在执行时是否打断进行取消，ture：打断子线程并抛出异常，没有返回值 、 false：不打断子线程并抛出异常，没有返回值
        futureTask.cancel(true);

        System.out.println("isCancelled：" + futureTask.isCancelled());

        Integer result = futureTask.get();

        System.out.println("after get isDone：" + futureTask.isDone());
        System.out.println("after get isCancelled：" + futureTask.isCancelled());

        System.out.println(result);

    }

    // 版本V2
   /* public static void main(String[] args) throws ExecutionException, InterruptedException {

        // FutureTask：一个FutureTask对象，线程只执行一次
        // FutureTask<Integer> futureTask = new FutureTask<>(new MyCallableThread());
        // Thread callable01 = new Thread(futureTask, "Callable01");
        // Thread callable02 = new Thread(futureTask, "Callable02");
        // callable01.start();
        // callable02.start();

        // FutureTask：两个FutureTask对象，线程只执行一次
        FutureTask<Integer> futureTask01 = new FutureTask<>(new MyCallableThread());
        FutureTask<Integer> futureTask02 = new FutureTask<>(new MyCallableThread());
        Thread callable01 = new Thread(futureTask01, "Callable01");
        Thread callable02 = new Thread(futureTask02, "Callable02");
        callable01.start();
        callable02.start();

        System.out.println("获取结果前");

        // get方法会阻塞当前线程
        Integer result01 = futureTask01.get();
        Integer result02 = futureTask02.get();
        System.out.println(result01);
        System.out.println(result02);

        System.out.println("获取结果后");

    }*/

    // 版本V1
    /*public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建多线程
        new Thread(new MyRunnableThread(), "Runnable").start();

        // 不能直接这样使用：new Thread(new MyCallableThread()).start();

        // FutureTask 未来任务
        FutureTask<Integer> futureTask = new FutureTask<>(new MyCallableThread());
        Thread callable = new Thread(futureTask, "Callable");
        callable.start();

        System.out.println("获取结果前");

        // get方法会阻塞当前线程
        Integer result = futureTask.get();
        System.out.println(result);

        System.out.println("获取结果后");

    }*/
}
